﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DTO.Models
{
    public class FeedbackProvider
    {
        CITDBDataContext _db = new CITDBDataContext();

        public void Save()
        {
            if (_db != null)
                _db.SubmitChanges();
        }

        public IQueryable<FEEDBACK> GetAll()
        {
            try
            {
                var result = _db.FEEDBACKs.OrderByDescending(b => b.FeedbackId);

                return result;
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
        }

        public IQueryable<FEEDBACK> GetAll(string email, DateTime pDateFrom, DateTime pDateTo, bool pIsAnswer)
        {
            try
            {
                var temp = _db.FEEDBACKs.Where(b => b.Date >= pDateFrom && b.Date <= pDateTo.AddDays(1) && b.IsAnswered == pIsAnswer);
                if (string.IsNullOrEmpty(email))
                    return temp.OrderByDescending(b => b.FeedbackId);
                else
                    return temp.Where(b => b.Email.Contains(email)).OrderByDescending(b => b.FeedbackId);
            }
            catch
            {
                return null;
            }
        }

        public IQueryable<FEEDBACK> GetAll(string email, DateTime pDateFrom, DateTime pDateTo)
        {
            try
            {
                var temp = _db.FEEDBACKs.Where(b => b.Date >= pDateFrom && b.Date <= pDateTo.AddDays(1));
                if (string.IsNullOrEmpty(email))
                    return temp.OrderByDescending(b => b.FeedbackId);
                else
                    return temp.Where(b => b.Email.Contains(email)).OrderByDescending(b => b.FeedbackId);
            }
            catch
            {
                return null;
            }
        }

        public void DeleteMark(string listId)
        {
            try
            {
                FEEDBACK obj = null;
                string[] arr = listId.Split('-');
                List<FEEDBACK> deleteItems = new List<FEEDBACK>();

                for (int i = 0; i < arr.Length - 1; i++)
                {
                    int id = int.Parse(arr[i]);
                    obj = _db.FEEDBACKs.SingleOrDefault(b => b.FeedbackId == id);
                    if (obj != null)
                        deleteItems.Add(obj);
                }

                _db.FEEDBACKs.DeleteAllOnSubmit(deleteItems);
                Save();
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
        }

        public void Answer(int feedbackId)
        {
            try
            {
                var feedback = _db.FEEDBACKs.SingleOrDefault(b => b.FeedbackId == feedbackId);
                if (feedback == null)
                    return;

                feedback.IsAnswered = true;
                Save();
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
        }

        public void Insert(FEEDBACK obj)
        {
            try
            {
                _db.FEEDBACKs.InsertOnSubmit(obj);

                Save();
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Message);
            }
        }
    }
}
